package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/14 22:10
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    //注入memberDao操作dao
    @Autowired
    private MemberDao memberDao;

    /*
    后台管理-->根据月份查询会员数量
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        List<Integer> list= new ArrayList<>();
        for (String month : months) {
            //拼接sql查询条件
            month = month + ".31";//2019.04.30
            //调用daowanchengchax
           Integer count =  memberDao.findMemberCountBeforeDate(month);
            list.add(count);
        }
        return list;
    }

    /*
        移动端-->手机端手机号和验证码快速登录
       HttpServletResponse:后台响应前台的数据
        1.根据手机号查询会员信息
        2.新增会员
        */
    @Override
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findByTelephone(telephone);
        return member;
    }

    @Override
    public void add(Member member) {
        if(member.getPassword() != null){//如果密码会员的密码不为空
            //调用MD5Utils密码加密工具类进行加密
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }
}
